/*
 * MineralActivity.cpp
 *
 *  Created on: 20 Aug 2011
 *      Author: Allan
 */

#include "MineralActivity.h"

// GeoReact includes
#include <Utils/Algorithms.h>

MineralActivity::MineralActivity() 
{}

MineralActivity::MineralActivity(const string& species, const vector<string>& phase_species, const ActivityCoefficientFunction& activity_coefficient) :
activity_coefficient(activity_coefficient)
{
	index_species = IndexOf(species, phase_species);
}

MineralActivity::~MineralActivity()
{}

const double MineralActivity::activity(double T, double P, const VectorXd& n) const
{
	const double molar_fraction = n[index_species]/n.sum();
	
	return molar_fraction * activity_coefficient(T, P, n);
}

const double MineralActivity::activityCoefficient(double T, double P, const VectorXd& n) const
{
	return activity_coefficient(T, P, n);
}

const VectorXd MineralActivity::activityDerivatives(double T, double P, const VectorXd& n, const VectorXd& a) const
{
	VectorXd derivatives = VectorXd::Zero(n.rows());
	
	derivatives[index_species] = a[index_species]/n[index_species];
	
	return derivatives;
}

const MineralActivity MineralActivityIdealModel(const string& species, const vector<string>& phase_species)
{
	// The ideal model for the mineral activity coefficient (gamma = 1.0)
	auto ideal_mineral_activity_coefficient = [](double T, double P, const VectorXd& n) { return 1.0; };
	
	MineralActivity mineral_activity(species, phase_species, ideal_mineral_activity_coefficient);
	
	return mineral_activity;
}
